package com.power.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.model.Result;
import com.power.domain.SysUser;
import com.power.domain.dto.SysUserDto;
import com.power.service.SysUserService;
import com.power.util.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService service;
    @GetMapping("/info")
    public Result info()
    {
        Long loginUserId = AuthUtils.getLoginUserId();
        SysUser byId = service.getById(loginUserId);
        return Result.success(byId);
    }
    @GetMapping("/info/{id}")
    public  Result getById(@PathVariable Long id)
    {
        SysUser byId = service.getByIdandRoleList(id);
        return Result.success(byId);
    }
    @GetMapping("/page")
    public Result page(@RequestParam Integer current ,@RequestParam Integer size,@RequestParam(required = false) String name)
    {
        Page<SysUser> page=new Page<>(current,size);
        Page pagelist = service.pagelist(page, name);
        return Result.success(pagelist);
    }
    @PostMapping
    public Result save(@RequestBody SysUserDto sysUser)
    {
        boolean b = service.saveOne(sysUser);
        return b ? Result.success("保存成功"):Result.fail(2000,"保存失败");
    }

    @PutMapping
    public  Result update(@RequestBody SysUserDto sysUserDto)
    {
        int i = service.usdateOne(sysUserDto);
        return i>0?Result.success("修改成功"):Result.fail(2000,"修改失败");
    }
    @DeleteMapping("/{ids}")
    public  Result  DeleteById(@PathVariable String ids)
    {
        int b = service.removeByLsitID(ids);
        return Result.success("成功删除"+b+"条数据");
    }

}
